//
//  MyaddController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/20.
//  Copyright © 2016年 game. All rights reserved.
//

#import "MyaddController.h"
#import "MyAddTableViewCell.h"
#import "MyAddModel.h"
#import "SpacePagerModel.h"

@interface MyaddController ()<UITableViewDataSource,UITableViewDelegate,MyAddTableViewCellDeleget>

@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,assign) CGFloat cellHight;

@property (nonatomic,strong) NSMutableArray *dataAry;
@property (nonatomic,assign) NSInteger nextpage;
@property (nonatomic,strong) MyAddModel *model;
@end

@implementation MyaddController

-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"MyAddTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    [self.view addSubview:self.tableview];
    self.tableview.separatorStyle = 0;
    self.tableview.backgroundColor = tabPlainBackColor;
    
    self.tableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self RefreshFooter];
    }];
    
    self.nextpage = 2;

    
    [self AchiveDataWhithPage:1];
}

-(void)RefreshFooter{
    
    SpacePagerModel *pageModel = self.model.pager;
    if (self.nextpage >pageModel.total_page.integerValue) {
        [self.tableview.footer noticeNoMoreData];
    }else
        [self AchiveDataWhithPage:self.nextpage];
    self.nextpage ++;
}


-(void)AchiveDataWhithPage:(NSInteger)page{
    
    
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:Mynotelist,page] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
//        NSLog(@"%@",responseObject);
        self.model = [[MyAddModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        [self.dataAry addObjectsFromArray:self.model.list];
        [self.tableview reloadData];
        
        [self.tableview.footer endRefreshing];
        
         SpacePagerModel *pageModel = self.model.pager;
        if (pageModel.total_page.integerValue<=1) {
            [self.tableview.footer removeFromSuperview];
        }
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    

    return self.cellHight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyAddTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataAry[indexPath.row];
    self.cellHight = cell.cellHight;
    cell.deleget = self;
    return cell;
}

-(void)ToSpaceWhithUid:(NSString *)uid{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.navigationController pushViewController:space animated:YES];
    
}

-(void)agreeWhithCell:(UITableViewCell *)cell AndMessage:(NSString *)message AndIsSucces:(BOOL)succes{

    [Manager GetHudWhithText:message AndFatherView:self.view];

    if (succes) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSIndexPath *indexPath = [self.tableview indexPathForCell:cell];
            
            [self.dataAry removeObjectAtIndex:indexPath.row];//移除数据源的数据
            [self.tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            
            if ((indexPath.row+1)>=self.dataAry.count) {
                [self AchiveDataWhithPage:self.nextpage];
            }
        });
  }

}

@end
